package connect4client.commands;

import java.util.List;

import game.Player;
import messages.ListPlayersReplyMessage;
import connect4client.core.PlayerManager;
import connect4client.user.User;

/**
 * The ListPlayersReply command.
 * 
 * This command will handle ListPlayersReply messages.
 */
public class ListPlayersReplyCommand extends Command {

	public ListPlayersReplyCommand(ListPlayersReplyMessage message){
		super(message);
	}
	
	@Override
	public void execute() {		
		ListPlayersReplyMessage message = (ListPlayersReplyMessage) this.message;
		PlayerManager playerManager = controller.getPlayerManager();
		User me = controller.getUser();
		
		synchronized (playerManager) {
		
			for (Player player : message.getPlayers()){			
				if (!me.getPlayer().equals(player)){
					playerManager.addPlayer(player.getLogin(),player);
				}	
			}		
		}
		
		guiMediator.listPlayersReply(message.getPlayers());
	}
}
